> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/jaypopat/cf_ai_duet/llms.txt
> Use this file to discover all available pages before exploring further.

# Server Package

> SSH server implementation for handling Duet connections

## Overview

The `server` package provides the SSH server that handles client connections and manages Bubble Tea sessions for the Duet TUI.

## Types

### Server

Main server struct that manages SSH connections and room state.

```go theme={null}
type Server struct {
    addr        string
    hostKeyPath string
    roomManager *room.Manager
    logger      *log.Logger
}
```

## Functions

### New

Creates a new Duet server instance.

```go theme={null}
func New(addr, hostKeyPath, workerURL string) *Server
```

<ParamField path="addr" type="string" required>
  The address to listen on (e.g., "localhost:2222")
</ParamField>

<ParamField path="hostKeyPath" type="string" required>
  Path to the SSH host key file
</ParamField>

<ParamField path="workerURL" type="string">
  Optional URL for the AI worker service. If empty, AI features will be disabled
</ParamField>

**Returns:** A configured `*Server` instance

**Example:**

```go theme={null}
srv := server.New("localhost:2222", ".ssh/duet_ed25519", "http://localhost:8787")
```

### Start

Starts the SSH server and blocks until shutdown.

```go theme={null}
func (s *Server) Start() error
```

**Returns:** Error if the server fails to start or during shutdown

**Example:**

```go theme={null}
if err := srv.Start(); err != nil {
    log.Fatal("Server error:", err)
}
```

**Behavior:**

* Starts SSH server with Bubble Tea middleware
* Listens for OS interrupt signals (SIGINT, SIGTERM)
* Performs graceful shutdown with 10-second timeout
* Logs server lifecycle events

## Implementation Details

### SSH Middleware

The server uses Wish middleware for SSH handling:

* **Bubble Tea Middleware**: Manages TUI sessions
* **Logging Middleware**: Logs connection events

### Session Handling

Each SSH connection:

1. Extracts username (defaults to "guest")
2. Creates a Bubble Tea renderer
3. Detects terminal type (special handling for xterm-ghostty)
4. Initializes a UI model with the room manager
5. Launches in alternate screen mode

### Color Profile Detection

```go theme={null}
if pty.Term == "xterm-ghostty" {
    renderer.SetColorProfile(termenv.TrueColor)
}
```

The server automatically enables TrueColor for Ghostty terminal.
